package com.ww.juc.jd.async.basic;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.jd.platform.async.callback.ICallback;
import com.jd.platform.async.callback.IWorker;
import com.jd.platform.async.worker.WorkResult;
import com.jd.platform.async.wrapper.WorkerWrapper;

import cn.hutool.core.date.SystemClock;
import cn.hutool.http.HttpUtil;

/**
 * 二十四节气worker
 * @author wanggw
 * 2024年2月19日 下午5:49:35
 */
public class TwentyFourWorker implements IWorker<String, String>, ICallback<String, String> {

	public static final String APP_KEY = "";

	public static final String URL = "https://cn.bing.com";

	/**
	 * 开始
	 */
	@Override
	public void begin() {
		System.out.println(Thread.currentThread().getName() + "--开始--" + System.currentTimeMillis());
	}

	/**
	 * 结果回调
	 */
	@Override
	public void result(boolean success, String param, WorkResult<String> workResult) {
		if (success) {
			System.out.println("回调二十四节气成功--" + SystemClock.now() + "--" + workResult.getResult() + "-threadName:" + Thread.currentThread().getName());
		} else {
			System.out.println("回调二十四节气失败--" + SystemClock.now() + "--" + workResult.getResult() + "-threadName:" + Thread.currentThread().getName());
		}
	}

	/**
	 * 处理具体任务的地方
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public String action(String object, Map<String, WorkerWrapper> allWrappers) {
		String url = URL;
		String result = HttpUtil.get(url);

		try {
			TimeUnit.SECONDS.sleep(12);
		} catch (InterruptedException e) {
			System.err.println("二十四节气异常：" + e.getMessage());
		}

		return result;
	}

	@Override
	public String defaultValue() {
		return "默认值";
	}

}
